package com.woniu.controller;

import com.woniu.common.utils.token.AccToken;
import com.woniu.common.utils.token.JwtTokenUtils;
import com.woniu.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 杨光 微信号:tolryg
 * @time 09:48
 */
@RestController
@CrossOrigin
public class UploadController {

    @Autowired
    UploadService uploadService;

    @Value("${server.port}")
    private String port;


    @RequestMapping("file/upload")
    public String upload(HttpServletRequest request, MultipartFile file){

        String authorization = request.getHeader("Authorization");

        if(authorization!=null && authorization.startsWith("Bearer")){
            String jwt = authorization.substring(7);
            //验证jwt是否有效
            AccToken accToken = JwtTokenUtils.parseToken(jwt);
            if(accToken==null){
                return "error:unauthorized";
            }
        }else{
            return "error:unauthorized";
        }

        System.out.println("port = " + port);

        String url = uploadService.upload(file);
        return url;
    }
}
